
#include "stdafx.h"
#include "Vsg_File_Op.h"
#include <fstream>

using namespace std;





void CVsg_File_OP::LoadVsgFile( const char* lpszFileName, PT3D& ptBBMin, PT3D& ptBBMax, BoxelIndex& resolution, double& dVxlSize, 
							DataMap& mapZXY, DataMap& mapXYZ, DataMap& mapYZX, VxlProp& vxlProp )
{
	fstream fFile( lpszFileName, ios::in );
	if( !fFile )
		return;

	//ptBBMin
	while( !fFile.eof() )
	{
		string	strTemp;
		fFile >> strTemp;
		if( strcmp( strTemp.c_str(), "Min:" ) == 0 )
		{
			fFile >> ptBBMin.x >> ptBBMin.y >> ptBBMin.z;
			break;
		}
	}

	//ptBBMax
	while( !fFile.eof() )
	{
		string	strTemp;
		fFile >> strTemp;
		if( strcmp( strTemp.c_str(), "Max:" ) == 0 )
		{
			fFile >> ptBBMax.x >> ptBBMax.y >> ptBBMax.z;
			break;
		}
	}

	//Resolution
	while( !fFile.eof() )
	{
		string	strTemp;
		fFile >> strTemp;
		if( strcmp( strTemp.c_str(), "Resolution(x,y,z):" ) == 0 )
		{
			fFile >> resolution.x >> resolution.y >> resolution.z;
			break;
		}
	}

	//dVxlSize
	while( !fFile.eof() )
	{
		string	strTemp;
		fFile >> strTemp;
		if( strcmp( strTemp.c_str(), "Size:" ) == 0 )
		{
			fFile >> dVxlSize;
			break;
		}
	}

	while( !fFile.eof() )
	{
		string	strTemp;
		fFile >> strTemp;
		if( strcmp( strTemp.c_str(), "Begin" ) == 0 )
			break;
	}
	//map< int, map< int, map< int, int >>>	mapZXY, mapXYZ, mapYZX;
	while( !fFile.eof() )
	{
		string strTemp;
		fFile >> strTemp;
		if( strcmp( strTemp.c_str(), "#" ) == 0 )
			break;

		//BoxelIndex vData;
		int nX, nY, nZ;
		int nCount = 0;
		nX = atoi( strTemp.c_str() );
		fFile >>  nY >> nZ >> nCount;

		/*stringstream ss;
		ss << nX << "_" << nY << "_" << nZ; */
		string	strMapKey = BoxelIndex( nX, nY, nZ ).FromVoxelIndexToKeyString();
		vector<string>	vProp;
		for( int i = 0; i < nCount; ++ i )
		{
			fFile >> strTemp;
			vProp.push_back( strTemp );
		}

		RecordVoxelInMap( nZ, nX, nY, mapZXY );
		RecordVoxelInMap( nX, nY, nZ, mapXYZ );
		RecordVoxelInMap( nY, nZ, nX, mapYZX );
		vxlProp.insert( VxlProp::value_type( strMapKey, vProp ) );
	}

	fFile.close();
}


void CVsg_File_OP::RecordVoxelInMap( int x1, int y1, int z1, DataMap& mapXYZ )
{
	map< int, map< int, int >>& map_x_index = mapXYZ.insert( make_pair( x1, map< int, map< int, int >>() ) ).first->second;
	map< int, int >& map_y_index = map_x_index.insert( make_pair( y1, map< int, int >()) ).first->second;
	map< int, int >::iterator it_find_z1 = map_y_index.find( z1 );
	if( it_find_z1 == map_y_index.end() )
		map_y_index.insert( map< int, int >::value_type( z1, 1 ) );
}
